package com.distribute.dto;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.distribute.entity.TSysAcl;
import com.distribute.entity.TSysUser;
import com.distribute.enums.UserStatus;
import com.fasterxml.jackson.annotation.JsonIgnore;

public class LoginUser extends TSysUser implements UserDetails {

	private static final long serialVersionUID = -1379274258881257107L;

	private List<TSysAcl> sysAcls;
	/** 登陆时间戳（毫秒） */
	private Long loginTime;
	/** 过期时间戳 */
	private Long expireTime;

	public List<TSysAcl> getSysAcls() {
		return sysAcls;
	}

	public void setSysAcls(List<TSysAcl> sysAcls) {
		this.sysAcls = sysAcls;
	}

	@Override
	@JsonIgnore
	public Collection<? extends GrantedAuthority> getAuthorities() {
		return sysAcls.stream().map(p -> new SimpleGrantedAuthority(p.getPermission())).collect(Collectors.toSet());
	}

	public void setAuthorities(Collection<? extends GrantedAuthority> authorities) {
		// do nothing
	}

	// 账户是否未过期
	@JsonIgnore
	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	// 账户是否未锁定
	@JsonIgnore
	@Override
	public boolean isAccountNonLocked() {
		return getStatus() != UserStatus.LOCKED.getCode();
	}

	// 密码是否未过期
	@JsonIgnore
	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	// 账户是否激活
	@JsonIgnore
	@Override
	public boolean isEnabled() {
		return true;
	}

	public Long getLoginTime() {
		return loginTime;
	}

	public void setLoginTime(Long loginTime) {
		this.loginTime = loginTime;
	}

	public Long getExpireTime() {
		return expireTime;
	}

	public void setExpireTime(Long expireTime) {
		this.expireTime = expireTime;
	}

	@Override
	public String toString() {
		return "LoginUser [sysAcls=" + sysAcls + ", loginTime=" + loginTime + ", expireTime=" + expireTime
				+ ", toString()=" + super.toString() + ", hashCode()=" + hashCode() + "]";
	}

	@Override
	public int hashCode() {
		return super.getUsername().hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		LoginUser other = (LoginUser) obj;
		if (!getUsername().equals(other.getUsername()))
			return false;
		return true;
	}
	
	
	
}
